//相关业务逻辑
$(function(){
	page.getWeiXinGroup();
	page.bindTag();
});

var dapartmentId=null;
var page={
		//绑定页面元素
		bindTag:function(){
			// 测试发送消息
			$("#sendMessage").bind("click",function(){
				page.sendMessage();
			});
			//添加部门
			$("#addDepartment").bind("click",function(){
				$("#createPeople").hide();
				$("#createDepartment").show();
			});
			//添加人员
			$("#addPeople").bind("click",function(){
				$("#createDepartment").hide();
				$("#createPeople").show();
			});
			//设置接收人员与完成
			$("#changeMsg").bind("click",function(){
				var text=$(this).text();
				if('完成'==text){//当按钮是完成时
					var peopleId=new Array();  
					$('input[name="toSendPeople"]:checked').each(function(){  
						peopleId.push($(this).val());//向数组中添加元素  
					});  
					if(peopleId.length>0){
					    page.addSend(peopleId);
					}
					$("#allPeoTab tr td input").addClass("hidden");
					$("#retu").addClass("hidden");
					$(this).text("设置接收消息人员");
				}else{//当按钮时设置接收消息人员时
					$("#allPeoTab tr td input").removeClass("hidden");
					$("input[name='toSendPeople']").attr("checked",false);
					$("#retu").removeClass("hidden");
					$(this).text("完成");
				}
			});
			
			//取消按钮
			$("#retu").bind("click",function(){
				$("#retu").addClass("hidden");
				$("#changeMsg").text("设置接收消息人员");
				$("#allPeoTab tr td input").addClass("hidden");
			});
		},
		bindTagUl:function(){
			 //当点部门时触发
			$("#queryWeiXinDiv ul li").bind("click",function(){
				var id =$(this).attr("id");
				var deptId=$(this).attr("value");
				dapartmentId=deptId;
				page.getWeiXinGroupPeople(id,deptId);
				page.getSendPeople();
				});
		},
//		获取部门方法
		getWeiXinGroup:function(workshopName,workingProcedureName){
			$.ajax({
				url:"weiXin/getWeiXinGroup", 
				success:function(data){
					if(data.errcode==0){
						var html="<ul>";
						var selectHtml="";
						var deptId="";
						var deptList=data.department;
						for(var i=0;i<deptList.length;i++){
							deptId=deptList[i].id;
						    html+="<a><li value='"+deptId+"' id="+i+">"+deptList[i].name+"</li></a></br>";
						    selectHtml+="<option value＝'"+deptId+"'>"+deptList[i].name+"</option>";
						}
						html+="</ul>";
						$("#queryWeiXinDiv").html(html);
						$(".fatherSelect").append(selectHtml);
					}else{
						$("#queryWeiXinDiv").html("没有数据");
					}
					page.bindTagUl();
			    }
			});
		},
		getWeiXinGroupPeople:function(id,deptId){
			$.ajax({
				url:'weiXin/getWeiXinGroupPeople',
				data:{
					'deptId':deptId
				},
				success:function(data){
					if(data.errcode==0){
						var html="<tr class='text-c'>";
						var userList=data.userlist;
						if(userList.length>0){
							for(var index in userList){
								if(index>0){
									if(index%6!=0){
										html+="<td> <input type='checkbox' name='toSendPeople' value='"+userList[index].userid+"' class='hidden' />"+userList[index].name+"</td>";
									}else{
										html+="</tr><tr class='text-c'><td> <input type='checkbox' name='toSendPeople' value='"+userList[index].userid+"' class='hidden' />"+userList[index].name+"</td>";
									}
								}else{
									html+="<td> <input type='checkbox' name='toSendPeople' value='"+userList[index].userid+"' class='hidden' />"+userList[index].name+"</td>";
								}
							}
							var mo=(userList.length-1)%6;
							if(mo!=0){
								for(var i=1;i<(6-mo);i++){
									html+="<td></td>";
								}
							}
						    $("#allCount").text(userList.length);
							$("#allPeoTab").html(html);
						}else{
							$("#allPeoTab").html("该部门下暂时没有成员");
						}
					}else{
						$("#allPeoTab").html("没有获取到数据");
					}
					$("#peopleListDiv").removeClass("hidden");
				}
			});
		},
		sendMessage:function(){
			$.ajax({
				url:'weiXin/sendMessage',
				success:function(data){
					console.log(data);
				}
			});
		},
		addSend:function(peopleId){
			var stringId=JSON.stringify(peopleId);
			$.ajax({
				url:'weiXin/addSend',
				data:{
					'stringId':stringId
				},
				success:function(data){
					if(data.result){
						layer.msg(data.data, {icon: 1});
						page.getSendPeople();
					}else{
						layAlert(data.info, {icon: 1});
					}
				}
			});
		},
		getSendPeople:function(){
			$.ajax({
				url:'weiXin/getSendPeople',
				data:{
					'dapartmentId':dapartmentId
				},
				success:function(data){
					if(data.result){
						data=data.data;
						if(data.length>0){
							var html="";
							for(var key in data){
								html+=data[key].name+"&nbsp;&nbsp;&nbsp;&nbsp;";
							}
							$("#sendPeo").html(html);
							$("#count").text(data.length);
						}else{
							$("#sendPeo").html("该部门没有需要接受企业微信信息人员");
						}
					}else{
						layAlert(data.info);
					}
				}
			})
			
		}
}

$('ul').click(function(){
    $(this).next('ul').toggle();
});

//封裝alert提示框
function layAlert(str){
	layer.alert(str, {
		  skin: 'layui-layer-lan' //样式类名
		  ,closeBtn: 1
		});
}

